package cardsbody {
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	
	import editor.*;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class pi_pjc extends pi_cardbody{
		private var container: *;
		private var c_size: Point; 
		protected var _label: uif_label;
		
		public function pi_pjc(parent:uif_component,
									ax: int=0, ay: int=0,
									awidth: int=0, aheight: int=0, styleName: String='') {
			super(parent, ax, ay, awidth, aheight, styleName);
			_photo.visible = false;
			_photo.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e: Event): void {
				if (container != null) _photo.duplicate(container.injectPhoto);
			});
			app.addEventListener(GDataEvent.EXTERNALDATA, doExternalData);
		}
		
		override public function get cardBounds(): Rectangle {
			return new Rectangle(container.x, container.y, c_size.x, c_size.y);
		}
		
		override protected function maskResize(): void {
			super.maskResize();
			if (container != null) {
				var inner		: Rectangle = getInner();
				var scale		: Point = new Point(inner.width / c_size.x, inner.height / c_size.y);
				var r_scale		: Number;
				
				if (scale.x < scale.y) r_scale = scale.x;
				else r_scale = scale.y;
				if (r_scale > 1) r_scale = 1;

				container.scaleX = r_scale;
				container.scaleY = r_scale;

				container.x 		= inner.x + (inner.width - c_size.x * r_scale) / 2;
				container.y 		= inner.y + (inner.height - c_size.y * r_scale) / 2;
			}
		}
		
		override public function applyPhotoMatrix(matrix: Matrix): void {
			super.applyPhotoMatrix(matrix);
			if (container) container.matrix = matrix; 
		}
		
		protected function disposeContainer(): void {
			if (container != null) {
				if (container.hasOwnProperty('dispose')) container.dispose();
				else removeChild(container);
					
				container = null;
			}
		}
		
		override protected function updateFromInfo(): void {
			if (container) {
				var result: ColorTransform = new ColorTransform();
				result.concat(_maskInfo.colors);
				result.concat(colorsExt);
				container.injectPhoto.transform.colorTransform = result;
				container.matrix = photoMatrix;
			}
			super.updateFromInfo();
		} 
		
		override public function unloadAll(): void {
			disposeContainer();
			super.unloadAll();
		}
		
		public function get containerSize(): Point {
			return c_size;
		}
		
		public function get Container(): * {
			return container;
		}
		
		override protected function doLoadMask(e: Event): void {
			c_size = new Point(card_mask.source.contentLoaderInfo.width, card_mask.source.contentLoaderInfo.height); // Размер берем из оригинального
			if (card_mask.source.numChildren > 0) {
				card_mask.source.getChildAt(0).visible = false;
			}
			disposeContainer();
			
			var PJContainer:* = card_mask.source.contentLoaderInfo.applicationDomain.getDefinition('PJContainer');
			container = new PJContainer(_maskInfo, _photoMatrix);
			addChild(container);
			
			if (card_photo.contentLoaderInfo.bytesLoaded == 0) {
				_label		= new uif_label(this, 0, 0, width, height, 'maskHelp');
				_label.text = pi_data.SELECTPHOTOHELP;
				card_photo.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e: Event): void {
					_label.visible = false;
				});
			} else card_photo.duplicate(container.injectPhoto);
//			applyPhotoMatrix(_photoMatrix?_photoMatrix:(new Matrix()));
			maskResize();
			super.doLoadMask(e);
		}
		
		protected function doExternalData(e: GDataEvent): void {
			app.addEventListener(MouseEvent.CLICK, e.data.mouseRelease);
			app.addEventListener(MouseEvent.MOUSE_MOVE, e.data.mouseMove);
			app.addEventListener(MouseEvent.MOUSE_DOWN, e.data.mousePress);
//			app.addEventListener(MouseEvent.MOUSE_OUT, e.data.mouseLeave);
		}
	}
}